
import java.io.BufferedReader;
import java.util.Vector;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Vinay
 */
public class Runner
{
    static Vector<String> gameParamList = new Vector<String>();
    static Game g = new Game();
            
    public static void getList()
    {
        try
        {
            BufferedReader br = g.readFile("list.txt");
            String line="";
            while((line=br.readLine())!=null)
            {
                gameParamList.add(line);
            }
        }
        catch(Exception ex)
        {
            g.writeFile("err.txt", ex.getMessage());
            ex.printStackTrace();
        }
    }

    public static void runConfigScreen()
    {
        g.free = false;
        ConfigScreen config = new ConfigScreen();
        while(!g.free);
        gameParamList = config.getGameParamsList();
    }

    public static void runInstructions(String game)
    {
        g.free = false;
        Instructions inst = new Instructions(game);
        while(!g.free);
    }

    public static void runImpulse(String params)
    {
        g.free=false;
        String paramPart[] = params.split(",");
        int numOfCoins = Integer.parseInt(paramPart[1]);
        double money = Double.parseDouble(paramPart[2]);
        int time = Integer.parseInt(paramPart[3]);
        System.out.println("Running Impulse");
        Impulse game = new Impulse(numOfCoins, money, time);
        while(!g.free);
    }

    public static void runBeautyContest(String params)
    {
        g.free=false;
        String paramPart[] = params.split(",");
        double factor = Double.parseDouble(paramPart[1]);
        int limit = Integer.parseInt(paramPart[2]);
        int participants = Integer.parseInt(paramPart[3]);
        System.out.println("Running BeautyContest");
        BeautyContest game = new BeautyContest(factor, limit, participants);
        while(!g.free);
    }

    public static void run2by2Matrix(String params)
    {
        g.free=false;
        String paramPart[] = params.split(",");
        int payoff[][] = new int[2][2];
        payoff[0][0] = Integer.parseInt(paramPart[1]);
        payoff[0][1] = Integer.parseInt(paramPart[2]);
        payoff[1][0] = Integer.parseInt(paramPart[3]);
        payoff[1][1] = Integer.parseInt(paramPart[4]);
        System.out.println("Running 2 by 2");
        CoordinationGame game = new CoordinationGame(payoff);
        game.setVisible(true);
        while(!g.free);
    }

    public static void runCentipede(String params)
    {
        g.free=false;
        String paramPart[] = params.split(",");
        int money = Integer.parseInt(paramPart[1]);
        int count = Integer.parseInt(paramPart[2]);
        Centipede game = new Centipede(money, count);
        while(!g.free);
    }

    public static void runCentipedePlayer2(String params)
    {
        g.free=false;
        String paramPart[] = params.split(",");
        double money = Double.parseDouble(paramPart[1]);
        Rev_Centipede game = new Rev_Centipede(money);
        while(!g.free);
    }

    public static void runTrust(String params)
    {
        g.free=false;
        String paramPart[] = params.split(",");
        double init_money = Double.parseDouble(paramPart[1]);
        Trust game = new Trust(init_money);
        while(!g.free);
    }

    public static void runTrustPlayer2(String params)
    {
        g.free=false;
        String paramPart[] = params.split(",");
        double p1_init_money = Double.parseDouble(paramPart[1]);
        Rev_Trust game = new Rev_Trust(p1_init_money);
        while(!g.free);
    }

    public static void runDictator(String params)
    {
        g.free=false;
        String paramPart[] = params.split(",");
        double init_money = Double.parseDouble(paramPart[1]);
        Dictator game = new Dictator(init_money);
        while(!g.free);
    }

    public static void runUltimatum(String params)
    {
        g.free=false;
        String paramPart[] = params.split(",");
        double money = Double.parseDouble(paramPart[1]);
        Ultimatum game = new Ultimatum(money);
        while(!g.free);
    }

    public static void runUltimatumPlayer2(String params)
    {
        g.free=false;
        String paramPart[] = params.split(",");
        double init_money = Double.parseDouble(paramPart[1]);
        Rev_Ulti game = new Rev_Ulti(init_money);
        while(!g.free);
    }

    public static void runRiskPreference(String params)
    {
        g.free=false;
        String paramPart[] = params.split(",");
        int money = Integer.parseInt(paramPart[1]);
        RiskPreference game = new RiskPreference(money);
        while(!g.free);
    }

    public static void main(String args[])
    {
//        try
//        {
//            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//        }
//        catch (UnsupportedLookAndFeelException e)
//        {
//           // handle exception
//        }
//        catch (ClassNotFoundException e)
//        {
//           // handle exception
//        }
//        catch (InstantiationException e)
//        {
//           // handle exception
//        }
//        catch (IllegalAccessException e)
//        {
//           // handle exception
//        }

        runConfigScreen();
        System.out.println("paramList size is : "+gameParamList.size());
        for(int i=0; i<gameParamList.size(); i++)
        {
            String linePart[] = gameParamList.get(i).split(",");
            if(linePart[0].equals("Impulse"))
            {
                runInstructions(linePart[0]);
                runImpulse(gameParamList.get(i));
            }
            else if(linePart[0].equals("2by2-Matrix"))
            {
                runInstructions(linePart[0]);
                run2by2Matrix(gameParamList.get(i));
            }
            else if(linePart[0].equals("Beauty-Contest"))
            {
                runInstructions(linePart[0]);
                runBeautyContest(gameParamList.get(i));
            }
            else if(linePart[0].equals("Risk-Preference"))
            {
                runInstructions(linePart[0]);
                runRiskPreference(gameParamList.get(i));
            }
            else if(linePart[0].equals("Centipede"))
            {
                runInstructions(linePart[0]);
                runCentipede(gameParamList.get(i));
            }
            else if(linePart[0].equals("Centipede-Player2"))
            {
                runInstructions(linePart[0]);
                runCentipedePlayer2(gameParamList.get(i));
            }
            else if(linePart[0].equals("Trust"))
            {
                runInstructions(linePart[0]);
                runTrust(gameParamList.get(i));
            }
            else if(linePart[0].equals("Trust-Player2"))
            {
                runInstructions(linePart[0]);
                runTrustPlayer2(gameParamList.get(i));
            }
            else if(linePart[0].equals("Dictator"))
            {
                runInstructions(linePart[0]);
                runDictator(gameParamList.get(i));
            }
            else if(linePart[0].equals("Ultimatum"))
            {
                runInstructions(linePart[0]);
                runUltimatum(gameParamList.get(i));
            }
            else if(linePart[0].equals("Ultimatum-Player2"))
            {
                runInstructions(linePart[0]);
                runUltimatumPlayer2(gameParamList.get(i));
            }
        }
        System.exit(0);
    }

}
